/**
 * 
 */
package de.cbf.cam.dsa.facet.armor.helper;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.util.uuidheader.entities.NamedEntity;
import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author salter
 *
 */
public class ZoneFactors extends NamedEntity implements ConstantsArmorZoneFactors{
	private static final Log logger = LogFactory.getLog(ZoneFactors.class);
	
	protected Map<String, Double> fZoneFactors;
	
	protected Boolean fStandard;
	
	public static Double getDefaultValueForZone(String zone){
		Double result = 0D;
		if(ZONE_HEAD.equals(zone)){
			return DEFAULT_FACTOR_RS_HEAD;
		}
		if(ZONE_BACK.equals(zone)){
			return DEFAULT_FACTOR_RS_BACK;
		}
		if(ZONE_CHEST.equals(zone)){
			return DEFAULT_FACTOR_RS_CHEST;
		}
		if(ZONE_STOMACH.equals(zone)){
			return DEFAULT_FACTOR_RS_STOMACH;
		}
		if(ZONE_ARM_LEFT.equals(zone)){
			return DEFAULT_FACTOR_RS_ARM_LEFT;
		}
		if(ZONE_ARM_RIGHT.equals(zone)){
			return DEFAULT_FACTOR_RS_ARM_RIGHT;
		}
		if(ZONE_LEG_LEFT.equals(zone)){
			return DEFAULT_FACTOR_RS_LEG_LEFT;
		}
		if(ZONE_LEG_RIGHT.equals(zone)){
			return DEFAULT_FACTOR_RS_LEG_RIGHT;
		}
		logger.error("default wanted unknown zone: "+zone);		
		return result;
	}
	
	/**
	 * 
	 */
	public ZoneFactors() {	
	}
	/**
	 * @param name
	 */
	public ZoneFactors(String name) {
		super(name);	
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.uuidheader.entities.NamedEntity#init()
	 */
	@Override
	protected void init() {
		super.init();
		fZoneFactors = new HashMap<String, Double>();
		fStandard = true;
		//
		resetAllZoneFactors();
	}
	/* (non-Javadoc)
	 * @see de.cbf.util.uuidheader.entities.UUIDHeader#doMergeDataFrom(de.cbf.util.uuidheader.entities.UUIDHeader, boolean)
	 */
	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		super.doMergeDataFrom(entity, i_am_older);
		if (entity instanceof ZoneFactors) {
			ZoneFactors other = (ZoneFactors) entity;
			if(i_am_older){
				this.fStandard = other.fStandard;
				this.fZoneFactors.clear();
				this.fZoneFactors.putAll(other.fZoneFactors);
			}
		}else{
			String mergeEntityClass = entity == null? "null":entity.getClass().getName();
			String errorMsg ="EntityToMerge "+mergeEntityClass+" does not fit class:"+this.getClass().getName();
			throw new EntityMergeException(errorMsg);
		}
	}
	
	public Boolean getStandard(){
		return fStandard;
	}

	protected void setStandard(Boolean newStandard) {
		if(newStandard == null || newStandard.equals(fStandard)) return;
		Boolean oldStandard = this.fStandard;
		this.fStandard = newStandard;
		propertyChangeSupport.fireIndexedPropertyChange(PN_Standard, PI_Standard,
				oldStandard,this.fStandard);	
	}	
	
	public Double getZoneFactor(String zoneName){
		return fZoneFactors.get(zoneName);		
	}
	
	public void replaceZoneFactor(String zoneName,Double value){
		fZoneFactors.put(zoneName, value);
		setStandard(checkStandard());
		propertyChangeSupport.fireIndexedPropertyChange(PN_ZoneFactor, PI_ZoneFactor,
				null,zoneName);
	}
	
	public void resetZoneFactor(String zoneName){
		if(ZONE_HEAD.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_HEAD);
		}
		if(ZONE_BACK.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_BACK);
		}
		if(ZONE_CHEST.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_CHEST);
		}
		if(ZONE_STOMACH.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_STOMACH);
		}
		if(ZONE_ARM_LEFT.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_ARM_LEFT);
		}
		if(ZONE_ARM_RIGHT.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_ARM_RIGHT);
		}
		if(ZONE_LEG_LEFT.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_LEG_LEFT);
		}
		if(ZONE_LEG_RIGHT.equals(zoneName)){
			fZoneFactors.put(zoneName, DEFAULT_FACTOR_RS_LEG_RIGHT);
		}
		propertyChangeSupport.fireIndexedPropertyChange(PN_ZoneFactor, PI_ZoneFactor,
				null,zoneName);
	}
	
	public void resetAllZoneFactors(){
		fZoneFactors.put(ZONE_HEAD, DEFAULT_FACTOR_RS_HEAD);
		fZoneFactors.put(ZONE_BACK, DEFAULT_FACTOR_RS_BACK);
		fZoneFactors.put(ZONE_CHEST, DEFAULT_FACTOR_RS_CHEST);
		fZoneFactors.put(ZONE_STOMACH, DEFAULT_FACTOR_RS_STOMACH);
		fZoneFactors.put(ZONE_ARM_LEFT, DEFAULT_FACTOR_RS_ARM_LEFT);
		fZoneFactors.put(ZONE_ARM_RIGHT, DEFAULT_FACTOR_RS_ARM_RIGHT);
		fZoneFactors.put(ZONE_LEG_LEFT, DEFAULT_FACTOR_RS_LEG_LEFT);
		fZoneFactors.put(ZONE_LEG_RIGHT, DEFAULT_FACTOR_RS_LEG_RIGHT);
		//
		setStandard(true);
		//
		propertyChangeSupport.fireIndexedPropertyChange(PN_AllZoneFactors, PI_AllZoneFactors,
				null,fZoneFactors);
	}
	
	public boolean checkZoneFactorIsDefault(String zoneName){
		if(ZONE_HEAD.equals(zoneName)){
			return DEFAULT_FACTOR_RS_HEAD.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_BACK.equals(zoneName)){
			return DEFAULT_FACTOR_RS_BACK.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_CHEST.equals(zoneName)){
			return DEFAULT_FACTOR_RS_CHEST.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_STOMACH.equals(zoneName)){
			return DEFAULT_FACTOR_RS_STOMACH.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_ARM_LEFT.equals(zoneName)){
			return DEFAULT_FACTOR_RS_ARM_LEFT.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_ARM_RIGHT.equals(zoneName)){
			return DEFAULT_FACTOR_RS_ARM_RIGHT.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_LEG_LEFT.equals(zoneName)){
			return DEFAULT_FACTOR_RS_LEG_LEFT.equals(fZoneFactors.get(zoneName));
		}
		if(ZONE_LEG_RIGHT.equals(zoneName)){
			return DEFAULT_FACTOR_RS_LEG_RIGHT.equals(fZoneFactors.get(zoneName));
		}
		logger.error("Check for Default wanted for unknown zone: "+zoneName);
		return false;
	}
	
	protected boolean checkStandard(){
		boolean result = true;
		result &= checkZoneFactorIsDefault(ZONE_HEAD);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_BACK);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_CHEST);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_STOMACH);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_ARM_LEFT);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_ARM_RIGHT);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_LEG_LEFT);
		if( ! result){
			return false;
		}
		result &= checkZoneFactorIsDefault(ZONE_LEG_RIGHT);		
		return result;
	}
}
